package com.heu.blood.autologous.controller;

import com.heu.blood.analysis.entity.BloodSampleInfoTransfusionEntity;
import com.heu.blood.application.exception.ApplicationExceptionEnum;
import com.heu.blood.application.vo.ApplicationSelectVo;
import com.heu.blood.application.vo.BloodApplicationVo;
import com.heu.blood.application.vo.CentFlowResultVo;
import com.heu.blood.autologous.entity.ApplicationStateInfoEntity;
import com.heu.blood.autologous.entity.BloodApplicationAutologousEntity;
import com.heu.blood.autologous.exception.AutologousExceptionEnum;
import com.heu.blood.autologous.service.BloodApplicationAutologousService;
import com.heu.blood.autologous.vo.*;
import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.vo.BloodInformationIdHistoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2024-11-27 17:50:46
 */
@RestController
@RequestMapping("autologous/bloodapplicationautologous")
public class BloodApplicationAutologousController {
    @Autowired
    private BloodApplicationAutologousService bloodApplicationAutologousService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "自体血模块", operate = "分页查询自体血申请单信息")
    public R list(@RequestBody BloodApplicationAutologousSelectVo bloodApplicationAutologousSelectVo){
        PageUtils page = bloodApplicationAutologousService.queryPage(bloodApplicationAutologousSelectVo);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "自体血模块", operate = "根据ID查询自体血申请单详情")
    public R info(@PathVariable("id") Long id){
		BloodApplicationAutologousEntity bloodApplicationAutologous = bloodApplicationAutologousService.getById(id);

        return R.ok().put("bloodApplicationAutologous", bloodApplicationAutologous);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "自体血模块", operate = "保存自体血申请单信息")
    public R save(@RequestBody BloodApplicationAutologousEntity bloodApplicationAutologous){
		bloodApplicationAutologousService.save(bloodApplicationAutologous);

        return R.ok();
    }

    @RequestMapping("/updateApplication")
    @ControllerLogAnnotation(module = "自体血模块", operate = "修改或者更新自体血申请单")
    public R updateApplication(@RequestBody BloodApplicationAutologousVo bloodApplicationAutologous){
        int flag = bloodApplicationAutologousService.updateApplication(bloodApplicationAutologous);
        if(flag == 0){
            return R.ok();
        }else if(flag == 1){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

    @RequestMapping("/removeApplicationAndItemsByIds")
    @ControllerLogAnnotation(module = "自体血模块", operate = "按照ID删除所有对应的申请单和申请单详情")
    public R removeApplicationAndItemsByIds(@RequestBody List<BloodApplicationAutologousEntity> bloodAutologousApplications){
        int flag = bloodApplicationAutologousService.removeApplicationAndItemsByIds(bloodAutologousApplications);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "自体血模块", operate = "修改自体血申请单信息")
    public R update(@RequestBody BloodApplicationAutologousEntity bloodApplicationAutologous){
		bloodApplicationAutologousService.updateById(bloodApplicationAutologous);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "自体血模块", operate = "批量逻辑删除自体血申请单信息")
    public R delete(@RequestBody Long[] ids){
		bloodApplicationAutologousService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 保存输血申请单（这个是实际用的）
     * @param bloodApplication
     * @return
     */
    @RequestMapping("/saveApplication")
    @ControllerLogAnnotation(module = "自体血模块", operate = "保存自体血输血申请单")
    public R saveApplication(@RequestBody BloodApplicationAutologousVo bloodApplication){
        int flag = bloodApplicationAutologousService.saveApplication(bloodApplication);
        if(flag == 0){
            return R.ok();
        }else if(flag == 1){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

    /**
     * 根据applicationId查询本条详细信息
     */
    @RequestMapping("/applicationinfo/{applicationId}")
    @ControllerLogAnnotation(module = "申请单模块", operate = "根据applicationId查询本条详细信息")
    public R applicationInfo(@PathVariable("applicationId") String applicationId){
        BloodApplicationAutologousVo bloodApplicationAutologousVo = bloodApplicationAutologousService.applicationInfo(applicationId);

        return R.ok().put("applicationInfo", bloodApplicationAutologousVo);
    }

    /**
     * 查询输血申请单详情
     * @param bloodApplicationVo
     * @return
     */
    @RequestMapping("/getApplicationInfo")
    @ControllerLogAnnotation(module = "申请单模块", operate = "查询输血申请单详情")
    public R getApplicationInfo(@RequestBody BloodApplicationAutologousVo bloodApplicationVo){
        BloodApplicationAutologousVo applicationInfo = bloodApplicationAutologousService.getApplicationInfo(bloodApplicationVo);
        return R.ok().put("data",applicationInfo);
    }

    @RequestMapping("/getBloodAutologousApplicationAndCollectRecords/{applicationId}")
    @ControllerLogAnnotation(module = "自体血模块", operate = "根据applicationId查询申请项和已采血的详细信息")
    public R getBloodAutologousApplicationAndCollectRecords(@PathVariable("applicationId") String applicationId){
        List<BloodApplicationAutologousItemAndCollectVo> result=bloodApplicationAutologousService.getBloodAutologousApplicationAndCollectRecords(applicationId);
       /* if (result.isEmpty()){
            throw new MyException(AutologousExceptionEnum.NON_EXISTENT_PARAM);
        }*/
        return R.ok().put("data",result);
    }

    @GetMapping("/generateApplicationId")
    @ControllerLogAnnotation(module = "自体血模块", operate = "自体血申请单生成时生成相应的applicationId")
    public R generateApplicationId(@RequestBody BloodApplicationAutologousEntity bloodApplicationAutologousEntity){
        int result = bloodApplicationAutologousService.generateAutologousApplicationId(bloodApplicationAutologousEntity);
        if (result==1){
            return R.ok();
        }else if(result==0){
            throw new MyException(ApplicationExceptionEnum.ILLEGAL_PARAMS);
        }
        return R.ok();
    }

    @RequestMapping("/centFlowList")
    @ControllerLogAnnotation(module = "自体血模块", operate = "查询输血流程中的列表")
    public R centFlowList(@RequestBody BloodApplicationAutologousSelectVo bloodApplicationAutologousSelectVo){
        AutologousCenterFlowResultVo centFlowResultVO = bloodApplicationAutologousService.centFlowList(bloodApplicationAutologousSelectVo);
        return R.ok().put("data",centFlowResultVO);
    }

    @RequestMapping("/getBloodApplicationAutologousState")
    @ControllerLogAnnotation(module = "自体血模块", operate = "获取自体血申请单的流程节点状态")
    public R getBloodApplicationAutologousState(String applicationId){
        List<ApplicationStateInfoEntity> applicationStateInfoEntities = bloodApplicationAutologousService.getBloodApplicationAutologousState(applicationId);
        return R.ok().put("data", applicationStateInfoEntities);
    }

    @RequestMapping("getApplicationItemAndBloodInformationByItemId")
    @ControllerLogAnnotation(module = "自体血模块", operate = "根据申请项id查询对应申请项和血液信息及bloodId")
    public R getApplicationAndBloodInformationByItemId(Long bloodapplicationItemId){
        BloodApplicationAutologousResultVo bloodApplicationAutologousResultVo=bloodApplicationAutologousService.getApplicationAndBloodInformationByItemId(bloodapplicationItemId);
        return R.ok().put("data", bloodApplicationAutologousResultVo);
    }
}
